import 'package:flutter/material.dart';
import 'home_page.dart';
import 'discover_page.dart';
import 'profile_page.dart';
import 'services_page.dart';

class MainNavigationScreen extends StatefulWidget {
  const MainNavigationScreen({super.key});

  @override
  State<MainNavigationScreen> createState() => _MainNavigationScreenState();
}

class _MainNavigationScreenState extends State<MainNavigationScreen> {
  int _currentIndex = 0;
  final PageController _pageController = PageController();

  // 底部导航栏项目
  final List<BottomNavigationItem> _bottomItems = [
    const BottomNavigationItem(
      icon: Icons.home_outlined,
      activeIcon: Icons.home,
      label: '首页',
      page: HomePage(),
    ),
    const BottomNavigationItem(
      icon: Icons.explore_outlined,
      activeIcon: Icons.explore,
      label: '发现',
      page: DiscoverPage(),
    ),
    const BottomNavigationItem(
      icon: Icons.widgets_outlined,
      activeIcon: Icons.widgets,
      label: '服务',
      page: ServicesPage(),
    ),
    const BottomNavigationItem(
      icon: Icons.person_outlined,
      activeIcon: Icons.person,
      label: '我的',
      page: ProfilePage(),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 顶部导航栏
      appBar: _buildAppBar(),

      // 页面内容
      body: PageView(
        controller: _pageController,
        physics: const NeverScrollableScrollPhysics(), // 禁用页面滑动
        children: _bottomItems.map((item) => item.page).toList(),
      ),

      // 底部导航栏
      bottomNavigationBar: _buildBottomNavigationBar(),
    );
  }

  // 构建顶部导航栏
  PreferredSizeWidget _buildAppBar() {
    return AppBar(
      title: Text(
        _bottomItems[_currentIndex].label,
        style: const TextStyle(
          fontWeight: FontWeight.w500,
          fontSize: 18,
        ),
      ),
      centerTitle: true,
      actions: [
        if (_currentIndex == 0) // 只在首页显示搜索图标
          IconButton(
            icon: const Icon(Icons.search, size: 24),
            onPressed: () {
              // 搜索功能
            },
          ),
        IconButton(
          icon: const Icon(Icons.notifications_outlined, size: 24),
          onPressed: () {
            // 通知功能
          },
        ),
      ],
    );
  }

  // 构建底部导航栏
  Widget _buildBottomNavigationBar() {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.1),
            blurRadius: 8,
            offset: const Offset(0, -2),
          ),
        ],
      ),
      child: SafeArea(
        child: Row(
          children: _bottomItems.asMap().entries.map((entry) {
            final index = entry.key;
            final item = entry.value;
            final isActive = index == _currentIndex;

            return Expanded(
              child: Material(
                color: Colors.transparent,
                child: InkWell(
                  onTap: () => _onItemTapped(index),
                  child: Container(
                    padding: const EdgeInsets.symmetric(vertical: 8),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Icon(
                          isActive ? item.activeIcon : item.icon,
                          color: isActive
                              ? Theme.of(context).primaryColor
                              : Colors.grey[600],
                          size: 24,
                        ),
                        const SizedBox(height: 4),
                        Text(
                          item.label,
                          style: TextStyle(
                            fontSize: 12,
                            color: isActive
                                ? Theme.of(context).primaryColor
                                : Colors.grey[600],
                            fontWeight:
                                isActive ? FontWeight.w500 : FontWeight.normal,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            );
          }).toList(),
        ),
      ),
    );
  }

  void _onItemTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
    _pageController.jumpToPage(index);
  }
}

class BottomNavigationItem {
  final IconData icon;
  final IconData activeIcon;
  final String label;
  final Widget page;

  const BottomNavigationItem({
    required this.icon,
    required this.activeIcon,
    required this.label,
    required this.page,
  });
}
